package com.toy.core.util;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.toy.core.config.SysConfig;
import com.toy.core.security.Authentication;

public class CookieUtils {
	
	public static void saveAuthentication(Authentication authentication,
			HttpServletRequest request,HttpServletResponse response){
		SysConfig sysConfig = SpringContextHolder.getBean(SysConfig.class);
		//实现cookie跨域
		//参考 http://www.javaeye.com/topic/664328
		response.setHeader("P3P","CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'");
		String ticket = (String)authentication.getAttribute(Authentication.ATTR_TICKET);
		Cookie ticketAsCookie = new Cookie(sysConfig.getCookieName(), ticket);
		ticketAsCookie.setMaxAge(-1);
		ticketAsCookie.setPath("/");
		response.addCookie(ticketAsCookie);
	}
	
	public static void removeAuthentication(HttpServletRequest request,HttpServletResponse response){
		SysConfig sysConfig = SpringContextHolder.getBean(SysConfig.class);
		Cookie ticketAsCookie = new Cookie(sysConfig.getCookieName(), null);
		ticketAsCookie.setMaxAge(0);
		ticketAsCookie.setPath("/");
		response.addCookie(ticketAsCookie);
	}
	
	public static void setCookie(HttpServletResponse response,
			String cookieName, String cookieDomain, String cookiePath,
			String cookieValue) {
		Cookie ticketAsCookie = new Cookie(cookieName, cookieValue);
		ticketAsCookie.setDomain(cookieDomain);
		if (StringUtils.isBlank(cookiePath)) {
			ticketAsCookie.setPath("/");
		} else {
			ticketAsCookie.setPath(cookiePath);
		}
		ticketAsCookie.setMaxAge(-1);
		response.addCookie(ticketAsCookie);
	}

	public static void setCookie(HttpServletResponse response, String sName,
			String sValue, int oExpires, String sPath, String sDomain,
			boolean bSecure) {
		Cookie cookie = new Cookie(sName, sValue);
		cookie.setDomain(sDomain);
		if (StringUtils.isBlank(sPath)) {
			cookie.setPath("/");
		} else {
			cookie.setPath(sPath);
		}
		cookie.setMaxAge(-1);
		response.addCookie(cookie);
	}
	
	public static Cookie getCookieByName(HttpServletRequest request,
			String sName) {
		Cookie[] cookies = request.getCookies();
		return getCookieByName(cookies, sName);
	}

	public static String getCookieValueByName(HttpServletRequest request,
			String name) {
		Cookie cookie = getCookieByName(request, name);
		return null == cookie ? null : cookie.getValue();
	}

	public static Cookie getCookieByName(HttpServletRequest request,
			String name, String domain) {
		Cookie[] cookies = request.getCookies();
		List<Cookie> domainCookies = new ArrayList<Cookie>();
		for (int i = 0; i < cookies.length; i++) {
			if (domain.equals(cookies[i].getDomain())) {
				domainCookies.add(cookies[i]);
			}
		}
		return getCookieByName((Cookie[]) domainCookies.toArray(), name);
	}

	public static Cookie getCookieByName(Cookie[] cookies, String name) {
		for (int i = 0; cookies != null && i < cookies.length; i++) {
			if (name.equals(cookies[i].getName())) {
				return cookies[i];
			}
		}
		return null;
	}
}
